﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using ExaminationSys.Helper;
using Ext.Net;

namespace ExaminationSys.View.SystemManage
{
    public partial class TeacherPrivilegeWindow : System.Web.UI.UserControl
    {
        private static readonly TeacherInfoService Proxy = ObjectFactory<TeacherInfoService>.Create();
        private static readonly TeacherPrivilegeRelService ProxyTeacherPrivilege = ObjectFactory<TeacherPrivilegeRelService>.Create();
        private static readonly SectionInfoService ProxySectionInfo = ObjectFactory<SectionInfoService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {
            var root = TreePanelTeacherPrivilege.GetRootNode();
            root.Collapse(true);
        }
        public Store GridStore { get; set; }
        public List<TeacherPrivilegeRel> TeacherPrivilegeRelCollection { get; set; }
        public List<SectionInfo> SectionInfoCollection { get; set; }
        public void SetData(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                X.Js.Call("setID", id);
            }
        }
        public void Show()
        {
            this.TeacherPrivilegeSetting.Show();
        }

        protected void GetNodes(object sender, NodeLoadEventArgs e)
        {
            if (e.NodeID.Equals("root", StringComparison.OrdinalIgnoreCase))
            {
                var idTmp = TeacherID.Value;
                Guid id = Guid.Empty;
                if (Guid.TryParse(idTmp, out id))
                {
                    var nodes = BuildTreeNodes(id,TreePanelTeacherPrivilege.Root);
                    e.Nodes = nodes;

                    if (nodes.Count == 0)
                    {
                        BtnAddNode.Disabled = true;
                    }
                }
            }
        }

      [DirectMethod]
        public string NodeLoad(string nodeID)
        {
            NodeCollection nodes = new Ext.Net.NodeCollection();


            var idTmp = TeacherID.Value;
            Guid id = Guid.Empty;
            if (Guid.TryParse(idTmp, out id))
            {
                var result = new NodeCollection();
                var items = ProxySectionInfo.GetPublishList();
                TeacherPrivilegeRelCollection = ProxyTeacherPrivilege.GetTeacherPrivilegeRelList(id);
                if (items == null) return "";
                SectionInfoCollection = items;
                TeacherPrivilegeRelCollection = TeacherPrivilegeRelCollection ?? new List<TeacherPrivilegeRel>();
                //TreePanelTeacherPrivilege.Root.Clear();

                foreach (var firstNode in items)
                {
                    Node node = new Node();
                    node.Text = firstNode.Name;
                    if (TeacherPrivilegeRelCollection.Any(r => r.SectionID == firstNode.ID))
                    {
                        node.Checked = true;
                    }
                    node.NodeID = firstNode.ID.ToString();
                    //node.IconCls = "icon-lightning";
                    node.Leaf = true;
                    result.Add(node);
                    TreePanelTeacherPrivilege.Root.Add(node);

                }

                return result.ToJson();
            }

            return "";
        }

        private NodeCollection BuildTreeNodes(Guid teacherId, NodeCollection nodes)
        {
            if (nodes == null)
            {
                nodes = new Ext.Net.NodeCollection();
            }
            var root = new Ext.Net.Node();
            root.Text = "Root";
            nodes.Add(root);
            var result = new NodeCollection();
            var items = ProxySectionInfo.GetPublishList();
            TeacherPrivilegeRelCollection = ProxyTeacherPrivilege.GetTeacherPrivilegeRelList(teacherId);
            if (items == null) return result;
            SectionInfoCollection = items;
            TeacherPrivilegeRelCollection = TeacherPrivilegeRelCollection ?? new List<TeacherPrivilegeRel>();
            //TreePanelTeacherPrivilege.Root.Clear();
            
            foreach (var firstNode in items)
            {
                Node node = new Node();
                node.Text = firstNode.Name;
                if (TeacherPrivilegeRelCollection.Any(r => r.SectionID == firstNode.ID))
                {
                    node.Checked = true;
                }
                node.NodeID = firstNode.ID.ToString();
                //node.IconCls = "icon-lightning";
                node.Leaf = true;
                result.Add(node);
                TreePanelTeacherPrivilege.Root.Add(node);
               
            }

            return nodes;
        }

        public void BtnSave_Click(object sender, DirectEventArgs e)
        {
            var obj = TeacherID.Value;
            var id = Guid.Empty;
            if (string.IsNullOrEmpty(obj))
            {
                MessageBoxHelper.ShowError("教师ID不能为空");
                return;
            }
            if (!Guid.TryParse(obj, out id))
            {
                MessageBoxHelper.ShowError("教师ID获取失败");
                return;

            }
            var collection = new List<TeacherPrivilegeRel>();
            var checkNodes = TreePanelTeacherPrivilege.CheckedNodes;
            foreach (var submittedNode in checkNodes)
            {

                var tpr = new TeacherPrivilegeRel();
                tpr.TeacherID = id;
                tpr.SectionID = new Guid(submittedNode.NodeID);
                collection.Add(tpr);
            }
            var flag = ProxyTeacherPrivilege.TryAddTeacherPrivilegeRel(obj, collection);
            if (flag)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                return;
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;
            }

        }
    }
}